import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

// 密钥对生成 http://web.chacuo.net/netrsakeypair

// const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
//   'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
//
// const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
//   '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
//   'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
//   'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
//   'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
//   'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
//   'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
//   'UP8iWi1Qw0Y='


const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzS+iilMJT4RyGMQUtQ/LbDzDFdiW94E0K/46VF8b9ZmSANrvVVmZ5a1B6u2l79Wf2JfL82a+17VLLSwj4vE+rXogHUw92krQmKVgPPNXVt9XTCKyesTm4G7yy8RU7EXDvvGxMp4K9G/EPszI2OL64UStoRUaiIiXatnZRUimxWwIDAQAB'

const privateKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPNL6KKUwlPhHIYxBS1D8tsPMMV2Jb3gTQr/jpUXxv1mZIA2u9VWZnlrUHq7aXv1Z/Yl8vzZr7XtUstLCPi8T6teiAdTD3aStCYpWA881dW31dMIrJ6xObgbvLLxFTsRcO+8bEyngr0b8Q+zMjY4vrhRK2hFRqIiJdq2dlFSKbFbAgMBAAECgYAPml+qBZZM4NP+eyhU1dW+XMQI9eHHwpnOScNwJl05pRZu7pOvQ+JAiTRZGqnSLFPlgluKAb/2D+lOZlqF8TW+rMeOYgHU+uVjFO9qbhbNvtSOUxQoQ/vVZilu7MSRIUd31g9G/L6GA2kczwmo0f7K6+P8Cc8XhPhDgLAyOX+NeQJBAPmuvcQe1irp/CtrBWx5xN7n6eiVV9zrgDX+xDHhnFoz/PEzwZiF02+nVVC1nuXHBdJS1TP6MfYpi7wEdh4vuVcCQQD5c82iqU2V/0clynsS2557mTzltfv2togs9GE+SduiT1X7dCuNV74gji0gvMw2ylNA2LNcoosW/DgEwbaN89GdAkEApsMrQ95EtQzqvCUSzkzWTPsgo5N7x7YTP/0VFSvWvfiTLJag6zoW6jAdL3be1iGDpeJlO/4TeJJvHfjs22SMqQJAbhjDUKCCospJn49PZJJTz19htkzsyQXk8lpltPUqH+f+Hf423qo0OvAlUMajJyeDOSKrfx4qX1mwa8EHjn1nLQJBAN0Ox8pkng7b/2BsJLYobMXciRIecyKDN5TzUxdZHNchn4MY+rMNffFXqVnGvCOw8muPO+XdSsdOhZLkEfTcE8w=dshKqsBenWcKO2wVHyOwX9TY1b6bAmQp'

// 加密
export function encrypt(data) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(publicKey) // 设置公钥
  // return encryptor.encrypt(txt) // 对数据进行加密

  // return encryptor.encrypt(txt) // 对数据进行加密
  // 将数据分成若干个小块，每个小块的长度为密钥长度 - 11
  var chunkSize = encrypt.getKey().n.bitLength() / 8 - 11;
  var dataChunks = [];
  for (var i = 0; i < data.length; i += chunkSize) {
    dataChunks.push(data.substr(i, chunkSize));
  }

  // 对每个小块进行加密，并将加密后的结果拼接起来
  var encrypted = '';
  for (var i = 0; i < dataChunks.length; i++) {
    encrypted += encrypt.encrypt(dataChunks[i]);
  }
  return encrypted;
}


function encryptNew(data) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(publicKey) // 设置公钥
  // return encryptor.encrypt(txt) // 对数据进行加密
  // 将数据分成若干个小块，每个小块的长度为密钥长度 - 11
  var chunkSize = encrypt.getKey().n.bitLength() / 8 - 11;
  var dataChunks = [];
  for (var i = 0; i < data.length; i += chunkSize) {
    dataChunks.push(data.substr(i, chunkSize));
  }

  // 对每个小块进行加密，并将加密后的结果拼接起来
  var encrypted = '';
  for (var i = 0; i < dataChunks.length; i++) {
    encrypted += encrypt.encrypt(dataChunks[i]);
  }
  return encrypted;
}





// 解密
export function decrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  let xx =encryptor.decrypt(txt)
  return xx // 对数据进行解密
}


export function decrypt2(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  let encryptedData = atob(txt)
  let decryptedData = ''
  let chunkSize = encryptor.getKey().n.bitLength() / 8
  for (let i = 0; i < encryptedData.length; i += chunkSize) {
    let chunk = encryptedData.slice(i, i + chunkSize)
    decryptedData += encryptor.decrypt(chunk)
  }
  return decryptedData // 对数据进行解密
}


// The error decryption code
export function decrypt3  (string) {
  var k = this.getKey();
  var maxLength = ((k.n.bitLength()+7)>>3);
  // var maxLength = 128;
  try {
    var string = b64tohex(string);
    var ct = "";
    if (string.length > maxLength) {
      var lt = string.match(/.{1,128}/g);
      lt.forEach(function(entry) {
        var t1 = k.decrypt(entry);
        ct += t1;
      });
    }
    var y = k.decrypt(b64tohex(string));
    return y;
  } catch (ex) {
    return false;
  }
};

